package com.file.system.plugin.shiro;

import cn.hutool.core.util.ArrayUtil;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class RoleOrFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        Subject subject = getSubject(request, response);
        String[] permissions = (String[]) mappedValue;

        // 判断是否是空数组
        if (ArrayUtil.isEmpty(permissions)) {
            return true;
        }

        // 判断是否是权限数组
        for (String permission : permissions) {
            // 判断是否是角色数组
            if (subject.hasRole(permission)) {
                return true;
            }
        }

        return false;
    }
}